package in.muton.edushare.panels.upload.single;

import in.muton.edushare.pages.common.EdusharePanel;
import in.muton.edushare.EduShareSession;

import java.util.List;

import org.apache.wicket.Page;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
import org.apache.wicket.model.IModel;

import com.edushare.common.vo.FileDirectories;
import com.edushare.common.vo.FilesUploaded;

public abstract class SingleUploadButtonPanel extends EdusharePanel {

	public SingleUploadButtonPanel(String id, IModel<FileDirectories> model) {
		super(id, model);
		final ModalWindow mWin = new ModalWindow("modalUploadWindow");
		mWin.setPageCreator(new ModalWindow.PageCreator() {
			
			public Page createPage() {
				return 
				new SingleUploadModalPage(mWin, 
						(IModel<FileDirectories>)SingleUploadButtonPanel
								.this.getDefaultModel());
				
			}
		});
		
		mWin.setCloseButtonCallback(new ModalWindow.CloseButtonCallback() {
			
			private static final long serialVersionUID = 147278083373076070L;

			public boolean onCloseButtonClicked(AjaxRequestTarget target) {
				onUploadWindowClose(target);
				return true;
			}
		});
		
		mWin.setWindowClosedCallback(new ModalWindow.WindowClosedCallback() {
			
			private static final long serialVersionUID = 1L;

			public void onClose(AjaxRequestTarget target) {
				onUploadWindowClose(target);
			}
		});
		
		mWin.setWidthUnit("px");
		mWin.setInitialWidth(350);
		mWin.setHeightUnit("px");
		mWin.setInitialHeight(200);
		
		add(mWin);
		
		add(new AjaxLink("showUploader") {
			
			private static final long serialVersionUID = 6024984340692274420L;

			@Override
			public void onClick(AjaxRequestTarget target) {
				//clean previously uploaded file every-time someone clicks to upload file
				EduShareSession.get().setFileUploadedInLastUpload((List<FilesUploaded>)null);
				mWin.show(target);
			}
			
		});
		
	}
	
	protected abstract void onUploadWindowClose(AjaxRequestTarget target);
	
	
}
